package com.lx.core.util;

import cn.hutool.core.util.ObjectUtil;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * @author lx
 * @date 2025/08/27 12:44
 **/
public class DateUtils {
    public static String formatStartTime(String timestampStr) {
        try {
            if (ObjectUtil.isEmpty(timestampStr)) {
                return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00:00:00";
            }
            long timestamp = Long.parseLong(timestampStr);
            LocalDate date = Instant.ofEpochMilli(timestamp)
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 00:00:00";
        } catch (NumberFormatException e) {
            // 如果已经是日期字符串格式，直接返回
            if (timestampStr.length() == 10) { // 假设是 yyyy-MM-dd 格式
                return timestampStr + " 00:00:00";
            }
            return timestampStr;
        }
    }

    public static String formatEndTime(String timestampStr) {
        try {
            if (ObjectUtil.isEmpty(timestampStr)) {
                return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 23:59:59";
            }
            long timestamp = Long.parseLong(timestampStr);
            LocalDate date = Instant.ofEpochMilli(timestamp)
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " 23:59:59";
        } catch (NumberFormatException e) {
            // 如果已经是日期字符串格式，直接返回
            if (timestampStr.length() == 10) { // 假设是 yyyy-MM-dd 格式
                return timestampStr + " 23:59:59";
            }
            return timestampStr;
        }
    }
}
